package com.hongxinguoji.manager.article.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hongxinguoji.manager.article.mapper.HxArticleDetailMapper;
import com.hongxinguoji.manager.article.mapper.HxArticleExtMapper;
import com.hongxinguoji.manager.article.mapper.HxArticleMapper;
import com.hongxinguoji.manager.article.pojo.HxArticle;
import com.hongxinguoji.manager.article.pojo.HxArticleDetail;
import com.hongxinguoji.manager.article.pojo.HxArticleExt;
import com.hongxinguoji.manager.article.service.IArticleService;
import com.hongxinguoji.manager.common.util.HXResult;

@Service
public class ArticleService implements IArticleService {

	@Autowired
	private HxArticleMapper articleMapper;

	@Autowired
	private HxArticleDetailMapper articleDetailMapper;

	@Autowired
	private HxArticleExtMapper articleExtMapper;

	@Override
	public void addArticle(HxArticle article, String content) {
		Long id = article.getId();
		Date date = article.getCreateTime();

		// 保存文章
		articleMapper.insert(article);

		// --------------------------------------------------------------
		// 保存文章详情

		HxArticleDetail articleDetail = new HxArticleDetail();
		articleDetail.setId(id);
		articleDetail.setContent(content);

		articleDetail.setCreateTime(date);
		articleDetail.setUpdateTime(date);
		// 访问量
		articleDetail.setCount((long) 0);

		// 插入数据
		articleDetailMapper.insert(articleDetail);

	}

	@Override
	public List<HxArticleExt> getArticleExtList() {

		// 查询所有数据
		List<HxArticleExt> list = articleExtMapper.getArticleExtList();
		return list;
	}

	@Override
	public void lockArticle(String id) {
		String tID = id.trim();
		// 转换id类型
		Long i = Long.parseLong(tID);
		HxArticle hxArticle = articleMapper.selectByPrimaryKey(i);
		// 逻辑删除操作（1，正常，0，删除）
		hxArticle.setStatus(0);
		// 更新操作
		articleMapper.updateByPrimaryKey(hxArticle);

	}

	@Override
	public void unlockArticle(String id) {
		String tID = id.trim();
		// 转换id类型
		Long i = Long.parseLong(tID);
		HxArticle hxArticle = articleMapper.selectByPrimaryKey(i);
		// 逻辑删除操作（1，正常，0，删除）
		hxArticle.setStatus(1);
		// 更新操作
		articleMapper.updateByPrimaryKey(hxArticle);
	}

	@Override
	public HxArticleExt getArticleExtById(Long id) {
		// 通过文章id，查询文章
		return articleExtMapper.getArticleExtById(id);

	}

	@Override
	public HXResult updateArticle(HxArticle article, String content) {

		Long id = article.getId();

		// 通过id查询的文章
		HxArticle hxArticle = articleMapper.selectByPrimaryKey(id);

		// 更新数据操作
		hxArticle.setCid(article.getCid());
		hxArticle.setTitle(article.getTitle());
		hxArticle.setEditor(article.getEditor());
		hxArticle.setSource(article.getSource());
		hxArticle.setSummary(article.getSummary());
		hxArticle.setImgUrl(article.getImgUrl());
		hxArticle.setVideoUrl(article.getVideoUrl());
		hxArticle.setKeywords(article.getKeywords());
		Date date = new Date();
		hxArticle.setUpdateTime(date);

		hxArticle.setCreateTime(article.getCreateTime());

		// 更新
		articleMapper.updateByPrimaryKey(hxArticle);

		// 更新文章详情
		if (StringUtils.isNoneBlank(content)) {
			// 文章详情不为空说明有修改
			HxArticleDetail articleDetail = articleDetailMapper.selectByPrimaryKey(id);
			articleDetail.setContent(content);
			articleDetail.setUpdateTime(article.getUpdateTime());

			// 更新
			articleDetailMapper.updateByPrimaryKey(articleDetail);

		}
		return HXResult.ok();
	}

	@Override
	public void deleteArticle(Long i) {
		articleMapper.deleteByPrimaryKey(i);
		articleDetailMapper.deleteByPrimaryKey(i);

	}

	@Override
	public List<HxArticleExt> getArticleExtListByProperty(String name, String value) {

		// 去掉两端空格
		name = name.trim();
		value = value.trim();
		List<HxArticleExt> list = null;

		// 查找id
		try {
			if ("id".equals(name)) {
				long id = Long.parseLong(value);
				list = articleExtMapper.getArticleExtListById(id);
			}
			return list;

		} catch (Exception e) {
			return list;
		}

	}

}
